package com.example.usedcardealer;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class DeleteCarsActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText etCjh;
    private Button btnSearch;
    private EditText etPp;
    private EditText etXh;
    private EditText etLx;

    private Button btnDelete;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deletecars);
        initView();
    }

    private void initView() {
        etCjh=(EditText) findViewById(R.id.et_cjh);
        btnSearch=(Button) findViewById(R.id.btn_search);
        etPp=(EditText)findViewById(R.id.et_pp);
        etXh=(EditText)findViewById(R.id.et_xh);
        etLx=(EditText)findViewById(R.id.et_lx);

        btnDelete= (Button) findViewById(R.id.btn_delete);
        //设置按钮的点击事件
        btnSearch.setOnClickListener((View.OnClickListener) this);
        btnDelete.setOnClickListener((View.OnClickListener) this);
    }

    @Override
    public void onClick(View v)
    {
        switch(v.getId())
        {
            case R.id.btn_search:   //查询操作
                searchOrder();
                break;
            case R.id.btn_delete:    //删除操作
                deleteOrder();
                break;
        }
    }
    //查找信息
    private void searchOrder() {
        String cjh=etCjh.getText().toString().trim();  //获取用户输入
        //建立数据库访问对象
        CarsDAO dao=new CarsDAO(getApplicationContext());
        //打开数据库
        dao.open();
        //调用数据库访问方法
        Car a=dao.getCars(cjh);
        //将数据填入控件
        etPp.setText(a.pp);
        etXh.setText(a.xh);
        etLx.setText(a.lx);

        //关闭数据库
        dao.close();
    }
    //删除信息
    private void deleteOrder() {
        Car a=new Car();
        a.cjh=etCjh.getText().toString().trim();
        //创建数据库访问对象
        CarsDAO dao=new CarsDAO(getApplicationContext());
        //打开数据库
        dao.open();
        //执行数据库访问方法
        int result= dao.deletCars(a);
        if(result>0) {
            Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show();
        }
        //关闭数据库
        dao.close();
    }
}

